package com.zhkeshe.keshe.Service;

import com.alibaba.fastjson.JSONObject;
import com.zhkeshe.keshe.entity.*;
import com.zhkeshe.keshe.mapper.DeviceMapper;
import com.zhkeshe.keshe.mapper.FengShanUseMapper;
import com.zhkeshe.keshe.mapper.UserDeviceMapper;
import com.zhkeshe.keshe.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import static com.zhkeshe.keshe.Socket.WebSocketServer.sendtoUUID;

@Service
public class UserDeviceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDeviceMapper userDeviceMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private FengShanUseMapper fengShanUseMapper;

    public boolean CommandHandle(Map<String,String> data) throws IOException {
        String deviceId=data.get("deviceId");
        String msg=data.get("message");
        System.out.println(msg);
        try {
            //tmp = {
            //						command:'4',
            //						speed:'3'
            //					};
            JSONObject jsonObject=JSONObject.parseObject(msg);
            Device device=deviceMapper.selectByUUID(deviceId);
            UserDevice userDevice=userDeviceMapper.selectByDeviceId(device.getId());
            FengShanUse fengShanUse=fengShanUseMapper.selectByUserID(Integer.parseInt(userDevice.getUserId()));
            if(fengShanUse==null){
                fengShanUse=new FengShanUse(Integer.parseInt(userDevice.getUserId()),0,0,0,0,0);
                fengShanUseMapper.insertFengShanUse(fengShanUse);
            }
            String command=jsonObject.getString("command");
            switch(command){
                case "3"://打开风扇，关闭风扇等
                    boolean method=jsonObject.getBoolean("method");
                    if(method){
                        fengShanUse.setUseOneNum(fengShanUse.getOpenNum()+1);
                        fengShanUseMapper.updateFengShanUse(fengShanUse);
                    }else {
                        fengShanUse.setCloseNum(fengShanUse.getCloseNum()+1);
                        fengShanUseMapper.updateFengShanUse(fengShanUse);
                    }
                    break;
                case "4":
                    String speed=jsonObject.getString("speed");
                    switch (speed){
                        case "1":
                            fengShanUse.setOpenNum(fengShanUse.getOpenNum()+1);
                            fengShanUseMapper.updateFengShanUse(fengShanUse);
                            break;
                        case "2":
                            fengShanUse.setUseTwoNum(fengShanUse.getUseTwoNum()+1);
                            fengShanUseMapper.updateFengShanUse(fengShanUse);
                            break;
                        case "3":
                            fengShanUse.setUseThreeNum(fengShanUse.getUseThreeNum()+1);
                            fengShanUseMapper.updateFengShanUse(fengShanUse);
                            break;
                    }
                    break;
            }
            sendtoUUID(deviceId,msg);
        } catch (Exception e) {
            throw e;
            //return false;
        }
        return true;
    }
    public CommonResult bind(Map<String,String> data){
        System.out.println(data+"UserDevice");
        String UserName=data.get("UserName");
        String DeviceUUID=data.get("DeviceUUID");
        String DeviceName=data.get("DeviceName");
        User user=userMapper.selectByUserName(UserName);
        Device device=deviceMapper.selectByUUID(DeviceUUID);
        if(device!=null){//设备已经注册
            return new CommonResult(true,"设备已经被注册",new ArrayList<>());
        }
        //新设备，先注册设备
        device=new Device();
        device.setDeviceUUID(DeviceUUID);
        device.setDeviceName(DeviceName);
        deviceMapper.insertDevice(device);

        device=deviceMapper.selectByUUID(DeviceUUID);
        //绑定用户
        System.out.println(device.getId()+"device id"+user.getId()+"userId");
        UserDevice userDevice=new UserDevice();
        userDevice.setDeviceId(device.getId()+"");
        userDevice.setUserId(user.getId()+"");
        userDeviceMapper.insertUserDevice(userDevice);
        return new CommonResult(true,"绑定成功",new ArrayList<>());
    }


}
